<?php

namespace app\front\model\menu;

use app\common\exception\BaseException;
use app\common\model\menu\Menu as MenuModel;
use app\common\model\info\Info as InfoModel;

/**
 * 文章模型
 */
class Menu extends MenuModel
{


    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'is_delete',
        'app_id',
        'update_time'
    ];

    /**
     * 所有栏目
     */
    public static function getHomeMenu($param)
    {
        $model = new static;
        $data = $model->with('mould')
            ->where('is_delete', '=', 0)
            ->where('status', '=', 1)
            ->order(['sort' => 'asc', 'create_time' => 'asc'])
            ->select();
        $all = !empty($data) ? $data->toArray() : [];
        $tree = [];
        foreach ($all as &$first) {
            if ($first['parent_id'] != 0) continue;
            $first['is_select'] = 0;
            if (isset($param['menu_id']) && $param['menu_id'] > 0) {
                $menuId = self::getSubMenuId($first['menu_id']);
                if (in_array($param['menu_id'], $menuId)) {
                    $first['is_select'] = 1;
                }
            }
            //链接
            $first['href'] = "/info/index";
            $twoTree = [];
            foreach ($all as &$two) {
                if ($two['parent_id'] != $first['menu_id']) continue;
                $two['href'] = "/info/index";
                array_push($twoTree, $two);
            }
            $first['child'] = [];
            if (!empty($twoTree)) {
                $temp_two_tree = array_column($twoTree, 'sort');
                array_multisort($temp_two_tree, SORT_ASC, $twoTree);
                $first['child'] = $twoTree;

            }
            array_push($tree, $first);
        }
        return $tree;
    }

    /**
     * 根据上级获取栏目
     */
    public function getMenu($parent_id,$limit=6)
    {
        $list = $this->where('parent_id', '=', $parent_id)
            ->where('is_delete', '=', 0)
            ->where('status', '=', 1)
            ->order(['sort' => 'asc', 'create_time' => 'asc'])
            ->select();
        foreach ($list as &$value) {
            $value['href'] = "/info/detail";
            $child = (new InfoModel)->where('menu_id', '=', $value['menu_id'])
                ->where('is_delete', '=', 0)
                ->where('status', '=', 1)
                ->limit($limit)
                ->select();
            foreach ($child as $item){
                if (mb_strlen($item['title'],'UTF8') > 12) {
                    $item['title'] = mb_substr($item['title'], 0, 12,'UTF8') . '...';
                }
                if (mb_strlen($item['desc'],'UTF8') > 35) {
                    $item['desc'] = mb_substr($item['desc'], 0, 35,'UTF8') . '...';
                }
            }
            $value['child'] = $child;
        }
        return $list;
    }

    /**
     * 根据上级获取栏目
     */
    public function getlevelMenu($parent_id, $menu)
    {
        if ($menu['topid'] != 0) {
            $parent_id = $menu['topid'];
        }
        $list = $this->where('parent_id', '=', $parent_id)
            ->where('is_delete', '=', 0)
            ->where('status', '=', 1)
            ->order(['sort' => 'asc', 'create_time' => 'asc'])
            ->select();
        return $list;
    }

    /**
     * 获取底部数据
     */
    public function getFooterData()
    {
        //关于我们数据
        $aboutUs = $this->where('menu_id', '=', 1)
            ->where('is_delete', '=', 0)
            ->where('status', '=', 1)
            ->find();
        $aboutUs['child'] = $this->getMenu($aboutUs['menu_id']);
        //产品中心数据
        $product = $this->where('menu_id', '=', 3)
            ->where('is_delete', '=', 0)
            ->where('status', '=', 1)
            ->find();
        $product['child'] = $this->getMenu($product['menu_id']);
        //新闻动态
        $news = $this->where('menu_id', '=', 2)
            ->where('is_delete', '=', 0)
            ->where('status', '=', 1)
            ->find();
        $news['child'] = $this->getMenu($news['menu_id']);
        //成功案例
        $case = $this->where('menu_id', '=', 4)
            ->where('is_delete', '=', 0)
            ->where('status', '=', 1)
            ->find();
        $case['child'] = $this->getMenu($case['menu_id']);
        $data['aboutUs'] = $aboutUs;
        $data['product'] = $product;
        $data['news'] = $news;
        $data['case'] = $case;
        return $data;
    }

    /**
     * 获取指定栏目下的所有子栏目id
     */
    public static function getSubMenuId($parent_id, $all = [])
    {
        $arrIds = [$parent_id];
        empty($all) && $all = self::getALLMenu();
        foreach ($all as $key => $item) {
            if ($item['parent_id'] == $parent_id) {
                unset($all[$key]);
                $subIds = self::getSubMenuId($item['menu_id'], $all);
                !empty($subIds) && $arrIds = array_merge($arrIds, $subIds);
            }
        }
        return $arrIds;
    }

    /**
     * 所有栏目
     */
    public static function getALLMenu()
    {
        $model = new static;
        $data = $model->with('mould')
            ->where('is_delete', '=', 0)
            ->where('status', '=', 1)
            ->order(['sort' => 'asc', 'create_time' => 'asc'])
            ->select();
        $all = !empty($data) ? $data->toArray() : [];
        return $all;
    }

}